<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="utf-8" lang="utf-8">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Dz2PunBB DB Converter</title>
<!-- stylesheet -->
<style>
.box{border: 2px solid #DCD9D0; width: 800px; height: 150px; overflow: auto}
.bold{font-weight: bold;}
.warnning{color: #BC4E2A;margin-bottom: 5px}
</style>
</head>
<body>
	
<?php
// Define root path
define('ROOTPATH', __FILE__);

// Include ezSQL core
include_once 'ezsql/shared/ez_sql_core.php';
// Include ezSQL database specific component
include_once 'ezsql/mysql/ez_sql_mysql.php';
// Include common functions
include_once 'includes/functions.php';
/*
// Discuz database configuretions
$dz_config = array
(
	'host' => 'localhost',		// databse address.(default is localhost)
	'user' => 'root',			// user name
	'password' => '',			// passowrd
	'database' => 'khnfans_forum',		// database name
	'prefix' => 'cdb_',			// database prefix
);

// Punbb database configuretions
$punbb_config = array
(
	'host' => 'localhost',		// databse address.(default is localhost)
	'user' => 'root',			// user name
	'password' => '',			// passowrd
	'database' => 'punbb',		// database name
	'prefix' => 'punbb_',		// database prefix
);
*/
if ( isset($_POST['action']) && $_POST['action']=='do')	
{
	echo '<h1>Discuz7 to PunBB1.3.3 Database Converter  </h1>';
		
	// Discuz database configuretions
	$dz_config = array
	(
		'host' => $_POST['source_host'],		// databse address.(default is localhost)
		'user' => $_POST['source_user'],			// user name
		'password' => $_POST['source_password'],			// passowrd
		'database' => $_POST['source_database'],		// database name
		'prefix' => $_POST['source_prefix'],			// database prefix
	);
	
	// Punbb database configuretions
	$punbb_config = array
	(
		'host' => $_POST['target_host'],		// databse address.(default is localhost)
		'user' => $_POST['target_user'],			// user name
		'password' => $_POST['target_password'],			// passowrd
		'database' => $_POST['target_database'],		// database name
		'prefix' => $_POST['target_prefix'],		// database prefix
	);
	
	@mysql_connect($dz_config['host'], $dz_config['user'], $dz_config['password']) or 
		die ('Discuz 7 database can\'t connect!');
		
	@mysql_connect($punbb_config['host'], $punbb_config['user'], $punbb_config['password']) or 
		die ('Punbb 1.3.3 database can\'t connect!');
	
	// Connect database
	$dz_db = new ezSQL_mysql($dz_config['user'], $dz_config['password'], $dz_config['database'], $dz_config['host']);
	$punbb_db = new ezSQL_mysql($punbb_config['user'], $punbb_config['password'], $punbb_config['database'], $punbb_config['host']);
	
	// Using charset utf8
	$dz_db->query("SET NAMES 'utf8'");
	$dz_db->query("SET CHARACTER SET 'utf8'");
	
	$punbb_db->query("SET NAMES 'utf8'");
	$punbb_db->query("SET CHARACTER SET 'utf8'");
	
	$dz_users = $dz_db->get_results("SELECT * FROM {$dz_config['prefix']}members", ARRAY_A); //LIMIT 1, 5
	//User password encrypt
	//***** Discuz7 *******
	//md5(md5($str).$salt);
	//******* PunBB ******* 
	//sha1($salt.sha1($str));
	if (sizeof($dz_users)>0)
	{
		echo 'Starting Convert users('.sizeof($dz_users).')...<br /><div class="box">';
		$count = 0;
		foreach ( $dz_users as $user )
		{
			$result = $punbb_db->query
			(
				"SELECT * FROM {$punbb_config['prefix']}users
				WHERE username='{$user['username']}' OR email='{$user['email']}'"
			);
			
			// goto next record if it found user in punbb database
			
			if ( $result!=0 ) 
			{
				echo '<small>user "'.$user['username'].'" was existed in database.</small><br />';
				continue;
			}
			
			$password = random_key(8, true);
			$salt = escape(random_key(12));
			$pwd_hash = password_hash($password, $salt);
			
			$punbb_db->query
			(
				"INSERT INTO {$punbb_config['prefix']}users(
					group_id, username, password, salt, email, title, realname, url,
					jabber, icq, msn, aim, yahoo, location, signature, disp_topics,
					disp_posts, email_setting, notify_with_post, auto_notify, show_smilies,
					show_img, show_img_sig, show_avatars, show_sig, access_keys, timezone,
					dst, time_format,date_format, language, style, num_posts, last_post,
					last_search, last_email_sent, registered, registration_ip, last_visit,
					admin_note, activate_string, activate_key
				)VALUES(
					3, '{$user['username']}', '{$pwd_hash}', '{$salt}', '{$user['email']}', NULL, NULL, NULL,
					NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
					NULL, 1, 0, 0, 1,
					1, 0, 1, 1, 0, 8,
					0, 0, 0, 'Simplified_Chinese', 'mootools', {$user['posts']}, {$user['lastpost']},
					NULL, NULL, {$user['regdate']}, '{$user['regip']}', {$user['lastvisit']},
					NULL, NULL, NULL
				)"
			);
			echo '<small class="bold">insert user "'.$user['username'].'" and password "'.$password.'" successful.</small><br />';
			
			$users[$count]['name'] = $user['username'];
			$users[$count]['password'] = $password;
			$users[$count]['email'] = $user['email'];
			$count++;
		}
		echo '</div>Finished Convert users<br /><br />';
	}
	else
	{
		echo '<div class="warnning">empty users in database</div>';
	}
	
	$dz_cats = $dz_db->get_results("SELECT * FROM {$dz_config['prefix']}forums", ARRAY_A); //LIMIT 1, 5
	if (sizeof($dz_cats)>0)
	{
		echo 'Starting Convert Categories...<br /><div class="box">';
		// Delete all data from 'categories' table
		$punbb_db->query
		(
			"DELETE FROM {$punbb_config['prefix']}categories"
		);
		
		// Reset AUTO_INCREMENT is 1.
		$punbb_db->query
		(
			"ALTER TABLE {$punbb_config['prefix']}categories AUTO_INCREMENT=1 ROW_FORMAT=DYNAMIC "
		);
		
		foreach( $dz_cats as $cat )
		{
			/*
			$result = $punbb_db->query
			(
				"SELECT * FROM {$punbb_config['prefix']}categories
				WHERE cat_name='{$cat['name']}'"
			);
			
			// goto next record if it found user in punbb database
			
			if ( $result!=0 ) 
			{
				echo '<small>Categorie "'.$cat['name'].'" was existed in database.</small><br />';
				continue;
			}
			*/
			if ( $cat['type']=='group' )
			{
				$punbb_db->query
				(
					"INSERT INTO {$punbb_config['prefix']}categories(
						id, cat_name, disp_position
					)VALUES(
						{$cat['fid']}, '{$cat['name']}', {$cat['displayorder']}+1
					)"
				);
				echo '<small class="bold">insert categorie "'.$cat['name'].'" successful.</small><br />';
			}
		}
		echo '</div>Finished Convert categories<br /><br />';
	}
	else
	{
		echo '<div class="warnning">empty categories in database</div>';
	}
	
	
	if (sizeof($dz_cats)>0)
	{
		echo 'Starting Convert Forums...<br /><div class="box">';
		
		// Delete all data from 'forums' table
		$punbb_db->query
		(
			"DELETE FROM {$punbb_config['prefix']}forums"
		);
		
		// Reset AUTO_INCREMENT is 1.
		$punbb_db->query
		(
			"ALTER TABLE {$punbb_config['prefix']}forums AUTO_INCREMENT=1 ROW_FORMAT=DYNAMIC"
		);
		
		foreach( $dz_cats as $cat )
		{
			if ( $cat['type']=='forum' )
			{
				$punbb_db->query
				(
					"INSERT INTO {$punbb_config['prefix']}forums(
						id, forum_name, forum_desc, num_topics, num_posts, 
						sort_by, disp_position, cat_id
					)VALUES(
						{$cat['fid']}, '{$cat['name']}', NULL, {$cat['threads']}, {$cat['posts']}, 
						0, {$cat['displayorder']}+1, {$cat['fup']}
					)"
				);
				echo '<small class="bold">insert categorie "'.$cat['name'].'" successful.</small><br />';
			}
		}
		echo '</div>Finished Convert Forums<br /><br />';
	}
	else
	{
		echo '<div class="warnning">empty forums in database</div>';
	}
	
	$dz_topics = $dz_db->get_results("SELECT * FROM {$dz_config['prefix']}threads", ARRAY_A); //LIMIT 1, 5
	
	if (sizeof($dz_topics)>0)
	{
		echo 'Starting Convert Topics('.sizeof($dz_topics).')...<br /><div class="box">';
		
		// Delete all data from 'forums' table
		$punbb_db->query
		(
			"DELETE FROM {$punbb_config['prefix']}topics"
		);
		
		// Reset AUTO_INCREMENT is 1.
		$punbb_db->query
		(
			"ALTER TABLE {$punbb_config['prefix']}topics AUTO_INCREMENT=1 ROW_FORMAT=DYNAMIC"
		);
		
		foreach( $dz_topics as $topic )
		{
			$sticky = empty($topic['displayorder']) ? 0 : 1 ;
			$punbb_db->query
			(
				"INSERT INTO {$punbb_config['prefix']}topics(
					id, poster, subject, 
					posted, first_post_id, last_post, last_post_id, 
					last_poster, num_views, num_replies, 
					closed, sticky, moved_to, forum_id
				)VALUES(
					{$topic['tid']}, '{$topic['author']}', '{$topic['subject']}', 
					{$topic['dateline']}, {$topic['authorid']}, {$topic['lastpost']}, 1, 
					'{$topic['lastposter']}', {$topic['views']}, {$topic['replies']}, 
					{$topic['closed']}, $sticky, NULL, {$topic['fid']}
				)"
			);
			echo '<small class="bold">insert topic "'.$topic['subject'].'" successful.</small><br />';
		}
		echo '</div>Finished Convert Topics<br /><br />';
	}
	else
	{
		echo '<div class="warnning">empty categories in database</div>';
	}
	
	$dz_posts = $dz_db->get_results("SELECT * FROM {$dz_config['prefix']}posts", ARRAY_A); //LIMIT 1, 5
	
	if (sizeof($dz_posts)>0)
	{
		echo 'Starting Convert Posts('.sizeof($dz_posts).')...<br /><div class="box">';
		
		// Delete all data from 'forums' table
		$punbb_db->query
		(
			"DELETE FROM {$punbb_config['prefix']}posts"
		);
		
		// Reset AUTO_INCREMENT is 1.
		$punbb_db->query
		(
			"ALTER TABLE {$punbb_config['prefix']}posts AUTO_INCREMENT=1 ROW_FORMAT=DYNAMIC"
		);
		
		foreach( $dz_posts as $post )
		{
			$message = escape(dz2punbb_bbcode($post['message']));
			
			$punbb_db->query
			(
				"INSERT INTO {$punbb_config['prefix']}posts(
					id, poster, poster_id, 
					poster_ip, poster_email, message, 
					hide_smilies, posted, edited, 
					edited_by, topic_id  
				)VALUES(
					{$post['pid']}, '{$post['author']}', {$post['authorid']}, 
					'{$post['useip']}', NULL, '{$message}', 
					0, {$post['dateline']}, NULL, NUll, {$post['tid']}
				)"
			);
			echo '<small class="bold">insert topic "'.$post['pid'].'" successful.</small><br />';
		}
		echo '</div>Finished Convert Posts<br /><br />';
	}
	else
	{
		echo '<div class="warnning">empty posts in database</div>';
	}
}
?>

<?php if (isset($_POST['action']) && $_POST['action']=='do'): ?>
	<div class="setting">
		All well done!
	</div>
<?php else: ?>
	<div class="setting">
		<form method="post" action="<?php echo $_SERVICE; ?>">
			<p>
				<h1>Discuz 7</h1>
				<input type="text" name="source_host" value="host" />
				<input type="text" name="source_user" value="user" />
				<input type="text" name="source_password" value="password" />
				<input type="text" name="source_database" value="database" />
				<input type="text" name="source_prefix" value="cdb_" />
			</p>
			<p>
				<h1>PunBB 1.3.x</h1>
				<input type="text" name="target_host" value="host" />
				<input type="text" name="target_user" value="user" />
				<input type="text" name="target_password" value="password" />
				<input type="text" name="target_database" value="database" />
				<input type="text" name="target_prefix" value="punbb_" />
			</p>
			<input type="hidden"name="action" value="do" />
			<input type="submit" value="Convert!" />
		</form>
	</div>
<?php endif; ?>
</body>
</html>
